package com.dianrui.firemenmonitor.activity;


import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dianrui.firemenmonitor.Constant;
import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.dialog.FlycoDialog.ExitDialog;
import com.dianrui.firemenmonitor.event.RreshDaliyAttendace;
import com.dianrui.firemenmonitor.event.RreshMy;
import com.dianrui.firemenmonitor.interfaces.OnAskDialogCallBack;
import com.dianrui.firemenmonitor.util.BadgerUtils;
import com.dianrui.firemenmonitor.util.GlideUtil;
import com.dianrui.firemenmonitor.util.MyUtil;
import com.dianrui.firemenmonitor.util.NoDoubleClickUtils;
import com.dianrui.firemenmonitor.util.OkHttpUtils;
import com.dianrui.firemenmonitor.util.StringUtils;
import com.dianrui.firemenmonitor.util.Url;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PersonCenterActivity extends BaseActivity {


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.act_my_msg_num)
    TextView actMyMsgNum;
    @BindView(R.id.pic)
    ImageView pic;
    @BindView(R.id.nickname)
    TextView nickname;
    @BindView(R.id.mobile)
    TextView mobile;
    @BindView(R.id.clockindays)
    TextView clockindays;
    @BindView(R.id.lateearlyleave)
    TextView lateearlyleave;
    @BindView(R.id.absence)
    TextView absence;
    @BindView(R.id.attendance_num)
    TextView attendanceNum;

    @Override
    public int getContentViewId() {
        return R.layout.activity_personcenter;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        customInit(R.color.blue_518bfa);
        title.setText(getString(R.string.person_title));
        if (!StringUtils.isEmpty(spUtils.getString("username"))) {
            nickname.setText(spUtils.getString("username"));
        }
        if(!StringUtils.isEmpty(spUtils.getString("userimg"))){
            GlideUtil.loadUserImageViewSize(getApplicationContext(), spUtils.getString("userimg"), pic);
        }
        if (!StringUtils.isEmpty(spUtils.getString("phone")) && spUtils.getString("phone").length() > 6) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < spUtils.getString("phone").length(); i++) {
                char c = spUtils.getString("phone").charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }
            mobile.setText(sb.toString());
        }
        getPersonInfo();
        getDaliyAttendce();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RreshMy rreshMy) {
        getPersonInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RreshDaliyAttendace rreshDaliyAttendacey) {
        getDaliyAttendce();
    }

    private void getPersonInfo() {
        OkHttpUtils.getInstance().postWithMemberToken(Url.PERSONCENTER, "", new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("code"))) {
                                if (!StringUtils.isEmpty(jsonObject.optJSONObject("data").optString("userimg"))) {
                                    GlideUtil.loadUserImageViewSize(getApplicationContext(), jsonObject.optJSONObject("data").optString("userimg"), pic);
                                }
                                nickname.setText(jsonObject.optJSONObject("data").optString("username"));

                                if (!StringUtils.isEmpty(jsonObject.optJSONObject("data").optString("phone")) && jsonObject.optJSONObject("data").optString("phone").length() > 6) {
                                    StringBuilder sb = new StringBuilder();
                                    for (int i = 0; i < jsonObject.optJSONObject("data").optString("phone").length(); i++) {
                                        char c = jsonObject.optJSONObject("data").optString("phone").charAt(i);
                                        if (i >= 3 && i <= 6) {
                                            sb.append('*');
                                        } else {
                                            sb.append(c);
                                        }
                                    }
                                    mobile.setText(sb.toString());
                                }
                            } else {

                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void getDaliyAttendce() {
        OkHttpUtils.getInstance().postWithMemberToken(Url.DALIYATTENDACE, "", new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("code"))) {
                                clockindays.setText(jsonObject.optJSONObject("data").optString("clockindays"));
                                lateearlyleave.setText(jsonObject.optJSONObject("data").optString("lateearlyleave"));
                                absence.setText(jsonObject.optJSONObject("data").optString("absence"));
                                attendanceNum.setText(jsonObject.optJSONObject("data").optString("attendance_num"));
                                int couts = jsonObject.optJSONObject("data").optInt("couts");
                                if (couts > 0) {
                                    BadgerUtils.addBadger(PersonCenterActivity.this, jsonObject.optJSONObject("data").optInt("couts"));
                                    actMyMsgNum.setVisibility(View.VISIBLE);//显示角标
                                    actMyMsgNum.setText(couts+"");
                                    if (couts>99) {
                                        actMyMsgNum.setVisibility(View.VISIBLE);
                                        actMyMsgNum.setText("99+");
                                    }
                                } else {
                                    BadgerUtils.removeCount(PersonCenterActivity.this);//移除角标
                                    actMyMsgNum.setVisibility(View.GONE);
                                    actMyMsgNum.setText(couts+"");
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (MyUtil.isAppIsInBackground(this)) {
            getDaliyAttendce();
        }
    }

    @OnClick({R.id.back, R.id.edit_person, R.id.right_img, R.id.more, R.id.feedback_layout, R.id.about_layout, R.id.updatepwd_layout, R.id.login_exitbtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.right_img:
                JumpActivitys(AlarmPromptActivity.class);
                break;
            case R.id.edit_person:
                JumpActivitys(EditPersonInfoActivity.class);
                break;
            case R.id.more:
                JumpActivitys(PunchStatisticsActivity.class);
                break;
            case R.id.feedback_layout:
                JumpActivitys(FeedBackActivity.class);
                break;
            case R.id.about_layout:
                JumpActivitys(AboutActivity.class);
                break;
            case R.id.updatepwd_layout:
                JumpActivitys(UpdatePasswordActivity.class);
                break;
            case R.id.login_exitbtn:
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    ExitDialog.createExitDialog(PersonCenterActivity.this, getString(R.string.login_out_info), new OnAskDialogCallBack() {
                        @Override
                        public void onokey(Dialog dialog) {
                            dialog.dismiss();
                            spUtils.clear();
                            JumpLoginActivitys(LoginActivity.class);
                        }

                        @Override
                        public void onCancel(Dialog dialog) {
                            dialog.dismiss();
                        }
                    });
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
